package com.shard.jdbc.config;

import com.alibaba.druid.pool.DruidDataSource;
import io.shardingsphere.api.config.rule.ShardingRuleConfiguration;
import io.shardingsphere.api.config.rule.TableRuleConfiguration;
import io.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 数据库分库分表配置
 */
@Configuration
public class ShardJdbcConfig {

    @Value("${spring.datasource.db1.druid.url}")
    private String dbUrl1;
    @Value("${spring.datasource.db1.druid.username}")
    private String username1;
    @Value("${spring.datasource.db1.druid.password}")
    private String password1;
    @Value("${spring.datasource.db1.druid.driverClassName}")
    private String driverClassName1;

    @Value("${spring.datasource.db2.druid.url}")
    private String dbUrl2;
    @Value("${spring.datasource.db2.druid.username}")
    private String username2;
    @Value("${spring.datasource.db2.druid.password}")
    private String password2;
    @Value("${spring.datasource.db2.druid.driverClassName}")
    private String driverClassName2;

    @Value("${spring.datasource.db3.druid.url}")
    private String dbUrl3;
    @Value("${spring.datasource.db3.druid.username}")
    private String username3;
    @Value("${spring.datasource.db3.druid.password}")
    private String password3;
    @Value("${spring.datasource.db3.druid.driverClassName}")
    private String driverClassName3;

    @Bean
    public DruidDataSource dataSource1() {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(dbUrl1);
        datasource.setUsername(username1);
        datasource.setPassword(password1);
        datasource.setDriverClassName(driverClassName1);
        return datasource;
    }


    @Bean
    public DruidDataSource dataSource2() {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(dbUrl2);
        datasource.setUsername(username2);
        datasource.setPassword(password2);
        datasource.setDriverClassName(driverClassName2);
        return datasource;
    }


    @Bean
    public DruidDataSource dataSource3() {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(dbUrl3);
        datasource.setUsername(username3);
        datasource.setPassword(password3);
        datasource.setDriverClassName(driverClassName3);
        return datasource;
    }

    /**
     * Shard-JDBC 分库配置
     */
    @Bean
    public DataSource dataSource () throws Exception {
        ShardingRuleConfiguration shardJdbcConfig = new ShardingRuleConfiguration();
        shardJdbcConfig.getTableRuleConfigs().add(getTableRule01());
        shardJdbcConfig.setDefaultDataSourceName("ds_1");
        Map<String,DataSource> dataMap = new LinkedHashMap<>() ;
        dataMap.put("ds_1",dataSource1()) ;
        dataMap.put("ds_2",dataSource2()) ;
        dataMap.put("ds_3",dataSource3()) ;
        Properties prop = new Properties();
        return ShardingDataSourceFactory.createDataSource(dataMap, shardJdbcConfig, new HashMap<>(), prop);
    }

    /**
     * Shard-JDBC 分表配置
     */
    private static TableRuleConfiguration getTableRule01() {
        TableRuleConfiguration result = new TableRuleConfiguration();
        result.setLogicTable("table_one");
        result.setActualDataNodes("ds_${1..3}.table_one_${1..5}");
        return result;
    }
}
